package com.chance.cc.crawler.development.bootstrap.douyin;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.downloader.*;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import com.chance.cc.crawler.development.utils.RedisReader;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;

import static org.codehaus.groovy.runtime.EncodingGroovyMethods.md5;

/**
 * @author bx
 * @date 2021/4/6 0006 11:16
 */
public class  DYTest1 {

    public static final String NULL_MD5_STRING = "00000000000000000000000000000000";
    public static final String byteTable = "D6 28 3B 71 70 76 BE 1B A4 FE 19 57 5E 6C BC 21 B2 14 37 7D 8C A2 FA 67 55 6A 95 E3 FA 67 78 ED 8E 55 33 89 A8 CE 36 B3 5C D6 B2 6F 96 C4 34 B9 6A EC 34 95 C4 FA 72 FF B8 42 8D FB EC 70 F0 85 46 D8 B2 A1 E0 CE AE 4B 7D AE A4 87 CE E3 AC 51 55 C4 36 AD FC C4 EA 97 70 6A 85 37 6A C8 68 FA FE B0 33 B9 67 7E CE E3 CC 86 D6 9F 76 74 89 E9 DA 9C 78 C5 95 AA B0 34 B3 F2 7D B2 A2 ED E0 B5 B6 88 95 D1 51 D6 9E 7D D1 C8 F9 B7 70 CC 9C B6 92 C5 FA DD 9F 28 DA C7 E0 CA 95 B2 DA 34 97 CE 74 FA 37 E9 7D C4 A2 37 FB FA F1 CF AA 89 7D 55 AE 87 BC F5 E9 6A C4 68 C7 FA 76 85 14 D0 D0 E5 CE FF 19 D6 E5 D6 CC F1 F4 6C E9 E7 89 B2 B7 AE 28 89 BE 5E DC 87 6C F7 51 F2 67 78 AE B3 4B A2 B3 21 3B 55 F8 B3 76 B2 CF B3 B3 FF B3 5E 71 7D FA FC FF A8 7D FE D8 9C 1B C4 6A F9 88 B5 E5";
    public static Logger logger = LoggerFactory.getLogger(DYTest1.class);
    public static final String hookApi = "http://192.168.1.214:8385";
    public static final String filePathName = "crawler-dev-bootstrap/src/main/java/com/chance/cc/crawler/development/bootstrap/douyin/devices.txt";

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-cla.abuyun.com");
        proxy.setPort(9030);
        proxy.setUsername("H3487178Q0I1HVPC");
        proxy.setPassword("ACE81171D81169CA");
    }


    public static void main(String[] args) throws IOException {

//        download();
//        device_register("3e8462aea946f793","6f7933e8462aea94","08:00:27:b1:8a:82");
//        copyRedisDataSet();

        try {
            register();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        device_register();

    }


    public static void register() throws InterruptedException {
        String filename = "crawler-dev-bootstrap/src/main/java/com/chance/cc/crawler/development/bootstrap/douyin/chushi.txt";
        List<String> infos = getContent(filename);
        for (String info : infos) {
            String[] split = info.split("\t");
            String serial = split[0];
            String openuuid = split[1];
            String macAddr = split[2];
            device_register(serial,openuuid,macAddr);
            System.out.println("注册" +
                    "完成" +
                    "");
            Thread.sleep(8000);

        }


    }
    public static List<String> getContent(String filepath)  {
        //读取文件
        List<String> lineLists = null;
        try {
            System.out.println(filepath);
            lineLists = Files
                    .lines(Paths.get(filepath), Charset.defaultCharset())
                    .flatMap(line -> Arrays.stream(line.split("\n")))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  lineLists;
    }

    private static void download(){
        HttpConfig httpConfig = HttpConfig.me("test");
//        httpConfig.setProxy(proxy);
//        httpConfig.setUseProxy(true);
//
        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        for (int i = 1; i <= 1; i++) {
            new Thread(new TestDevicesRun(downloader, httpConfig), "thread_" + i).start();
        }
    }

    private static Map<String,String> dySignature(HttpClientDownloader downloader,String dy_url,Map<String,String> dataMap){
        String signatureUrl = "http://192.168.1.214:8383/frida/dy/signature";
        HttpConfig dySignatureConfig = HttpConfig.me("test2");
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl(signatureUrl);
        Map<String,Object> dyDataMap = new HashMap<>();
        dyDataMap.put("dy_url",dy_url);
        dyDataMap.put("map_data_json",JSON.toJSONString(dataMap));
        httpRequest.setRequestBody(HttpRequestBody.form(dyDataMap,"utf-8"));
        httpRequest.setMethod("POST");
        String rawText = null;
        HttpPage download = null;
        try {
            download = downloader.download(httpRequest, dySignatureConfig);
            rawText = download.getRawText();
            if(rawText.contains("DOCTYPE HTML PUBLIC")){
                throw new Exception("DOCTYPE HTML PUBLIC");
            }
        } catch (Exception e) {
            try {
                while (download == null || !download.isDownloadSuccess()){
                    Thread.sleep(10000);
                    download = downloader.download(httpRequest, dySignatureConfig);

                    rawText = download.getRawText();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return JSON.parseObject(rawText,Map.class);
    }

    private static String dyRegister(HttpClientDownloader downloader,String str_data){
        String registerUrl = hookApi+ "/frida/dy/register";
        HttpConfig dyRegisterConfig = HttpConfig.me("test2");
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl(registerUrl);
        Map<String,Object> dyDataMap = new HashMap<>();
        dyDataMap.put("str_data",str_data);
        httpRequest.setRequestBody(HttpRequestBody.form(dyDataMap,"utf-8"));
        httpRequest.setMethod("POST");
        String rawText = null;
        HttpPage download = null;
        try {
            download = downloader.download(httpRequest, dyRegisterConfig);
            rawText = download.getRawText();
            if(rawText.contains("DOCTYPE HTML PUBLIC")){
                throw new Exception("DOCTYPE HTML PUBLIC");
            }
        } catch (Exception e) {
            try {
                while (download == null || !download.isDownloadSuccess()){
                    Thread.sleep(10000);
                    download = downloader.download(httpRequest, dyRegisterConfig);
                    rawText = download.getRawText();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return rawText;
    }

    static class TestDevicesRun implements Runnable {

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestDevicesRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }


        @Override
        public void run() {

            String url = "https://log3-misc.amemv.com/service/2/device_register/?is_activated=1&ac=wifi&channel=huawei_1&aid=1128&app_name=aweme&version_code=140100&version_name=14.1.0&device_platform=android&ssmix=a&device_type=Redmi 7&device_brand=xiaomi&language=zh&os_api=28&os_version=9&openudid=63112910997daff9&manifest_version_code=140101&resolution=720x1520&dpi=480&update_version_code=14109900&appTheme=dark&mcc_mnc=46011&cpu_support64=true&host_abi=arm64-v8a&app_type=normal&cdid=8d003b4d-cf05-4e6d-9f96-1354eb57ca1b&oaid=450d0ce7368f204b&tt_data=a";
            String byteStr = "116,99,5,16,0,0,-76,-114,-64,15,118,-96,127,12,-64,66,-66,121,113,-54,-64,-124,73,-49,-2,19,120,-25,-65,91,-64,-117,80,-110,73,23,-31,-68,70,84,-34,61,24,80,-29,38,-7,16,84,97,-15,95,-21,-107,-64,-85,-43,-4,-88,-20,-77,-27,89,44,-12,-57,-36,-94,-99,-50,0,-14,-48,97,45,-108,28,-27,-1,113,92,123,-13,-97,-6,-118,87,-10,-23,114,45,-102,120,-68,-10,55,28,-127,26,-6,-52,54,-54,92,-43,-18,48,-81,-123,93,-31,-73,34,13,-109,-6,-122,-66,84,-51,76,-10,-80,86,-32,5,-89,121,91,-34,-79,82,109,92,8,57,116,67,5,-71,127,-14,84,-120,-121,74,91,-55,42,-84,13,41,35,20,-15,-19,81,-28,-24,-47,-121,-48,58,67,-116,69,79,-15,18,-19,11,-113,112,-122,-8,-128,-94,42,-64,90,93,42,-112,107,-34,-66,-46,-12,-8,56,40,-75,-25,-59,-126,-18,96,124,12,-102,109,-9,-124,-36,-90,52,115,-48,32,-81,47,-48,114,-41,-84,-79,5,-109,50,90,70,-82,69,101,73,69,33,-71,49,71,28,-85,33,-18,24,-119,-106,63,-17,-62,89,-28,15,-62,-15,-66,-55,-39,60,82,-25,2,-12,-107,103,-128,-96,-72,19,120,-56,126,17,-76,10,26,-50,-57,-89,-11,62,-3,-51,40,-100,9,10,-8,-4,-58,-106,-4,-39,40,-87,28,74,118,52,-65,-51,31,97,78,-27,109,2,42,-33,57,-113,-55,-55,24,66,4,-16,40,-102,6,35,-126,111,-110,-44,4,19,84,-61,123,-82,-92,22,-1,-14,72,-31,25,-112,14,-67,-93,9,51,-45,102,22,97,47,86,22,50,-75,-61,-120,59,-99,105,31,-66,-16,-91,97,11,-38,51,-124,-37,-116,-75,-109,-84,102,32,-82,90,-29,-100,-26,-123,62,-76,-11,-121,73,-54,38,-63,-39,71,-46,-33,74,40,14,-63,8,-99,1,58,47,-40,75,-77,-82,-17,23,-61,83,84,-6,-101,-76,-115,-97,18,-13,-9,61,-16,-52,37,97,113,88,-128,-36,-34,-21,-29,84,37,-103,-86,-14,26,-69,80,-73,25,63,31,-1,78,60,-33,-40,12,52,-5,60,127,45,-53,38,-59,109,-57,13,28,-76,-85,-101,-4,3,87,-40,24,82,94,-107,82,38,63,-116,96,42,10,121,-37,58,63,34,-94,9,-117,-81,-6,-104,27,-64,6,46,126,34,78,-5,-73,-94,84,55,-30,2,-122,-95,98,65,-53,52,-64,-94,88,-63,27,37,64,8,-76,-9,-103,-79,-35,-88,101,3,65,48,-82,115,-73,4,-44,-105,-118,28,-28,-12,-21,6,84,-80,65,29,56,53,73,-66,-99,2,-23,-123,-31,48,-111,-35,-72,123,-58,-116,-2,74,115,64,-14,-18,-63,113,120,9,115,-117,88,-21,107,-57,-22,-9,-10,-126,68,7,-34,-30,-64,-36,44,-32,-108,43,119,-30,25,-55,48,42,-114,-61,116,-24,46,-70,-75,14,-64,-2,-102,-69,54,-60,61,-70,108,-64,59,16,61,-90,-1,37,4,-127,33,-115,-62,-117,25,-109,123,14,111,35,-120,29,0,-105,-64,125,112,-55,-83,-43,-108,-6,-53,-26,5,-28,19,-111,-102,30,-64,46,117,-22,21,-101,118,-73,17,51,-122,-23,105,123,74,65,124,-78,-96,23,78,-13,-121,10,-73,92,-80,127,-10,-15,35,-126,-62,-98,-9,-33,91,33,-33,-74,69,115,33,-3,-18,99,63,-109,125,-13,-71,-103,-26,97,-61,3,-33,20,-86,-37,-117,-67,-62,-118,-45,20,126,56,101,-67,13,31,87,61,-35,122,-4,95,102,-50,-37,-34,-117,87,45,-53,122,46,-48,33,18,-55,-102,-4,-45,-102,-100,-125,-127,-11,-21,-88,-95,64,88,-41,-98,-37,-84,32,-69,69,-38,39,123,17,127,106,106,-13,-48,-78,76,-52,60,66,3,-93,-121,91,67,57,-78,-78,-28,42,-109,-80,-40,127,-23,123,73,-94,-99,84,113,-44,-50,-14,-35,70,33,17,67,89,78,-110,-41,-77,-38,24,-127,-105,84,-85,-72,97,57,-96,-24,-52,-67,-35,48,62,-51,113,87,65,43,-46,73,50,93,-103,26,75,77,-85,-72,-16,-21,31,85,51,3,79,111,6,6,4,-32,14,-42,16,101,116,-58,-60,85,118,58,-53,-2,39,37,-5,-112,-28,117,-12,102,-64,-56,123,72,12,4,-119,69,59,118,22,-81,-36,-16,-25,60,127,124,-13,-113,26,37,-12,-101,-56,127,116,-120,82,-81,61,103,54,76,-126,40,-117,-48,110,106,67,-105,-15,-71,-113,86,-10,125,-40,62,-40,68,76,-28,18,107,46,109,94,-76,-29,-35,-11,36,-39,-89,25,41,-30,6,124,40,66,-61,-110,81,77,-53,-28,-51,-113,36,-69,119,-39,58,51,-29,-55,-116,92,37,-110,105,92,-11,120,120,-101,70,-85,122,44,17,106,72,-30,94,95,100,18,51,-29,72,-71,5,96,-82,-95,31,-69,97,-100,115,64,18,-125,101,-117,79,118,-80,-93,-97";
            String[] split = byteStr.split(",");
            byte[] bytes = new byte[split.length];
            for (int i = 0; i < split.length; i++) {
                bytes[i] = (byte)Integer.parseInt(split[i]);
            }
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod("POST");
            httpRequest.setUrl(url);
            httpRequest.setRequestBody(HttpRequestBody.custom(bytes,"application/octet-stream;tt-data=a","utf-8"));

            Map<String,String> dataMap = Maps.newHashMap();
            try {
                String stub = md5(bytes);
//                dataMap.put("x-ss-stub",md5(bytes));
                Map<String, String> signatureMap = dySignature(downloader, url, dataMap);
                httpRequest.addHeader("User-Agent", "com.ss.android.ugc.aweme/140101 (Linux; U; Android 8.0.0; zh_CN; MI 5s; Build/OPR1.170623.032; Cronet/TTNetVersion:1c8b77ac 2020-12-16 QuicVersion:47946d2a 2020-10-14)");
                httpRequest.addHeader("Content-Type", "application/octet-stream;tt-data=a");
                httpRequest.getHeaders().putAll(signatureMap);
                httpRequest.addHeader("X-SS-STUB", stub);

            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            HttpPage download = downloader.download(httpRequest, httpConfig);
            System.out.println(download.getRawText());
        }

//            deviceRegist(downloader,httpConfig,null,null);
    }

    private static String deviceRegist(HttpClientDownloader downloader, HttpConfig httpConfig,String url, String byteStr){
        httpConfig.setHttpSite("devices");
        String[] split = byteStr.split(",");
        byte[] bytes = new byte[split.length];
        for (int i = 0; i < split.length; i++) {
            bytes[i] = (byte)Integer.parseInt(split[i]);
        }
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setMethod("POST");
        httpRequest.setUrl(url);
        httpRequest.setRequestBody(HttpRequestBody.custom(bytes,"application/octet-stream;tt-data=a","utf-8"));

        Map<String,String> dataMap = Maps.newHashMap();
        try {
            String stub = md5(bytes);
//                dataMap.put("x-ss-stub",md5(bytes));
            Map<String, String> signatureMap = dySignature(downloader, url, dataMap);
//            httpRequest.addHeader("User-Agent", "com.ss.android.ugc.aweme/140101 (Linux; U; Android 8.0.0; zh_CN; MI 5s; Build/OPR1.170623.032; Cronet/TTNetVersion:1c8b77ac 2020-12-16 QuicVersion:47946d2a 2020-10-14)");
            httpRequest.addHeader("User-Agent", "com.ss.android.ugc.aweme/140101 (Linux; U; Android 7.1.2; zh_CN; SM-G9550;" +
                    " Build/OPR1.170623.032; Cronet/TTNetVersion:"+RandomStringUtils.random(8,"0123456789abcdf")+" 2020-12-16 QuicVersion:"+RandomStringUtils.random(8,"0123456789abcdf")+" 2020-10-14)");
            httpRequest.addHeader("Content-Type", "application/octet-stream;tt-data=a");
            httpRequest.getHeaders().putAll(signatureMap);
            httpRequest.addHeader("X-SS-STUB", stub);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String rawText = null;
        HttpPage download = null;
        try {
            download = downloader.download(httpRequest,httpConfig);
            if (!download.isDownloadSuccess()){
                throw new Exception("download error!");
            }
            rawText = download.getRawText();
        } catch (Exception e) {
            try {
                while (download == null || !download.isDownloadSuccess()){
                    Thread.sleep(10000);
                    download = downloader.download(httpRequest, httpConfig);
                    rawText = download.getRawText();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return rawText;
    }

    static class TestStartRun implements Runnable {

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestStartRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }


        @Override
        public void run() {
            String urlFormat = "https://api3-core-c.amemv.com/aweme/v2/feed/?type=0&max_cursor=0&min_cursor=0&count=6" +
                    "&volume=0.0&pull_type=4&need_relieve_aweme=0&filter_warn=0&req_from=enter_auto&is_cold_start=1" +
                    "&longitude=0&latitude=0&address_book_access=2&gps_access=2&cached_item_num=0&last_ad_show_interval=-1" +
                    "&preload_aweme_ids&download_sdk_info=%%7B%%22space_unoccupied%%22%%3A41260252%%7D&action_mask=-1" +
                    "&action_mask_detail=%%7B%%7D&teen_protector_vote_aweme_count=0&last_teen_protector_vote_aweme_interval=0" +
                    "&sp=-1&is_order_flow=0&user_avatar_shrink=96_96&screen_type=1&need_personal_recommend=1&live_auto_enter=0" +
                    "&pitaya_type=0&location_permission=false&manifest_version_code=140101&_rticket=1621927877373&app_type=normal" +
                    "&channel=huawei_1&device_type=%s&language=zh&cpu_support64=true&host_abi=%s&resolution=%s" +
                    "&update_version_code=14109900&cdid=%s&appTheme=dark&os_api=%s&dpi=480&ac=wifi" +
                    "&mcc_mnc=46011&os_version=%s&version_code=140100&app_name=aweme&version_name=14.1.0&device_brand=%s" +
                    "&ssmix=a&device_platform=android&aid=1128&ts=1621927877";
            String url = String.format(urlFormat,"DIG-AL00","arm64-v8a","720x1280","7f926aa2-0748-429d-a8fb-24f797b642dd",
                    "23","6.0","HUAWEI");

            HttpRequest httpRequest = new HttpRequest();

            httpRequest.setUrl(url);

            Map<String,String> dataMap = Maps.newHashMap();
            Map<String, String> signatureMap = dySignature(downloader, url, dataMap);
            httpRequest.addHeader("User-Agent", "com.ss.android.ugc.aweme/140101 (Linux; U; Android 8.0.0; zh_CN; MI 5s;" +
                    " Build/OPR1.170623.032; Cronet/TTNetVersion:1c8b77ac 2020-12-16 QuicVersion:47946d2a 2020-10-14)");
            httpRequest.getHeaders().putAll(signatureMap);
            HttpPage download = downloader.download(httpRequest, httpConfig);
            System.out.println(download.getRawText());
        }
    }


    static class TestDevicesLogRun implements Runnable {

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestDevicesLogRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }


        @Override
        public void run() {
            String urlFormat = "https://log3-misc-lq.amemv.com/service/2/app_log/?iid=%s" +
                    "&device_id=%s&ac=wifi&channel=huawei_1&aid=1128&app_name=aweme&version_code=140100" +
                    "&version_name=14.1.0&device_platform=android&ssmix=a&device_type=%s&device_brand=%s" +
                    "&language=zh&os_api=%s&os_version=%s&openudid=%s&manifest_version_code=140101" +
                    "&resolution=%s&dpi=480&update_version_code=14109900" +
                    "&appTheme=dark&mcc_mnc=46011&cpu_support64=true&host_abi=%s&app_type=normal" +
                    "&cdid=%s&oaid=%s&tt_data=a";
            String url = String.format(urlFormat,"1073581946582414","1636531873525054","DIG-AL00","HUAWEI",
                    "23","6.0","d637cb657b7c09ea","720x1280","arm64-v8a","7f926aa2-0748-429d-a8fb-24f797b642dd","f2171a1e23f3e7d8");
            String byteStr = "116,99,5,16,0,0,-96,65,-63,69,-85,-5,8,43,-67,-60,78,0,-74,-12,-40,-14,86,33,31,-23,-52,-30,104,26,-35,-82,1,-71,-83,-30,25,-53,21,-105,-24,-113,-48,106,84,45,-91,103,-6,-47,-64,-85,58,-48,-73,13,65,46,-93,107,4,-58,113,52,41,42,36,83,-112,-16,32,89,11,67,23,29,73,-45,51,-27,-58,89,100,-84,-70,1,22,123,47,-32,-114,-17,102,43,-74,-18,-18,-97,-3,-70,-11,109,-124,67,-52,-21,82,-47,-48,78,77,99,93,-71,8,-33,-34,58,102,-109,-14,-36,-97,-60,38,-80,86,98,59,-85,-27,98,27,-110,56,10,-89,-56,33,33,81,124,-41,50,27,-84,-17,50,-54,37,-53,45,-37,-81,-21,105,81,27,-34,96,30,75,20,36,93,-106,-44,37,-91,-81,-43,18,14,0,-5,-54,57,4,-54,121,-40,-2,114,-112,-125,-92,-96,64,-66,43,-50,-103,10,97,-39,19,-32,70,-6,23,125,41,-72,103,-48,-127,97,82,56,102,-67,-51,-46,-81,111,98,-125,89,-82,99,-38,-93,57,-72,72,-49,-61,-56,38,-107,-115,122,-68,36,-61,-75,81,-36,114,-60,27,-24,113,-67,-78,40,-5,-76,-32,-42,-51,88,86,-20,18,7,14,37,78,-104,-118,-14,-120,-124,80,12,-13,109,-90,59,38,-94,-58,39,-102,55,-95,91,-51,113,-113,16,-25,26,97,-29,-85,-64,74,-25,115,79,94,14,-20,-72,-83,-56,-31,-14,-102,-107,105,-127,26,-82,-42,-89,-116,73,-41,-117,14,-110,15,62,-49,104,-23,34,-121,63,60,-14,-16,-11,69,-79,-82,18,-9,-94,-79,4,14,-38,30,48,7,-21,-53,21,-8,38,93,29,-67,-68,127,-77,-34,-68,-32,44,87,109,-62,47,78,-100,93,104,-38,35,107,-64,-102,-33,20,58,74,70,124,-42,9,-97,-92,114,-113,117,90,51,13,32,-33,-127,3,-53,-69,-118,122,71,-70,90,46,63,119,22,4,-87,-43,-92,64,-70,30,-53,-46,15,-72,124,32,56,-45,-64,-12,11,19,72,86,1,4,-124,94,-60,-82,-120,79,-47,41,-73,45,100,-67,-44,61,-12,-76,73,102,-11,116,-58,112,19,61,75,76,-82,-36,54,-89,101,-4,56,26,-103,-25,-48,92,109,3,-17,-128,-22,-106,-36,-45,-127,53,-15,-95,33,26,-41,-91,48,58,-109,-84,-80,-121,59,50,107,-97,-80,-91,103,104,-71,79,-55,89,113,104,-111,101,26,42,84,12,89,-126,-29,122,-64,115,-49,127,-52,114,-15,1,24,-49,52,-39,-2,90,43,-87,75,34,-4,77,39,-17,-111,107,-71,-93,43,-63,-104,-97,85,-70,107,-70,49,-108,32,-37,23,-113,-103,-111,113,-60,-85,-60,40,-59,22,40,114,-100,-49,-124,-94,-44,117,-88,96,-18,-71,-2,-76,88,61,6,-13,50,4,-45,-31,-43,-34,92,-76,66,-111,-27,-67,-111,125,9,-111,-84,75,-121,32,117,-66,27,-122,-79,-98,70,-126,39,-125,-5,-93,-1,-120,-72,-57,107,39,-63,102,86,71,-38,86,112,93,42,1,34,-33,45,79,124,-94,110,44,-117,109,-82,-126,-92,62,-122,93,-54,-106,45,-46,-4,86,113,-38,-106,43,-14,-35,-72,24,87,-18,-79,9,-49,6,-63,18,-38,-93,72,24,-43,-5,-22,-42,21,99,-54,-13,34,39,48,-53,-56,124,-115,12,51,82,58,-60,100,-56,-2,-32,115,-91,-40,79,-82,-37,-126,71,22,-87,-9,124,-28,-102,-30,34,-94,-46,17,31,-34,50,-107,-77,112,106,16,-63,-55,104,0,4,37,18,-67,-15,-25,-17,61,-33,-119,46,-91,0,-114,-127,79,74,127,-38,-27,55,69,-80,-20,-58,-45,108,-42,-106,45,-12,-9,10,-51,-113,40,74,-26,90,-69,32,20,55,-56,101,-97,-89,85,122,-52,27,-85,-5,71,39,99,-20,-47,109,-19,-40,-25,-10,97,-37,86,-35,-100,-30,-18,0,-31,33,72,-19,0,-6,-99,29,-33,16,27,68,101,96,-104,49,9,95,103,-110,-99,98,-98,31,-127,-91,-51,-103,23,-12,8,42,105,-29,21,-100,-49,110,31,-74,-78,-114,108,54,-49,-128,97,-16,38,26,-96,18,-38,31,-111,-29,-11,-39,-95,0,-7,-109,103,60,-106,15,-97,82,106,-9,25,-12,-23,-97,-103,-24,27,32,4,-25,55,75,-86,-54,85,66,-41,14,95,-72,39,81,-2,81,-21,87,-115,123,-12,7,-52,-81,-112,103,21,66,5,59,119,101,-48,-96,44,45,-76,28,89,87,-121,-28,125,51,-103,7,-14,93,-111,50,-92,28,123,89,-75,-123,-30,-121,-38,-25,23,67,-48,-48,-48,80,-74,42,55,53,-12,-28,59,43,-103,-15,-103,-23,16,-19,-30,78,-32,-46,36,98,51,49,-22,-98,57,102,14,-24,-106,-115,-76,58,-3,28,99,119,-98,-31,-114,-14,79,58,71,-85,55,5,-82,115,-126,-6,33,-33,71,94,-1,-18,54,-87,103,-16,32,47,36,55,13,92,-11,100,-13,-55,-18,-90,-86,92,-52,124,91,-47,-50,90,-100,-117,-49,-49,101,-5,-28,11,-31,-128,-12,-60,-112,65,-37,-47,-107,-58,-46,89,96,-103,-97,99,75,26,39,-87,-18,86,-96,-104,-50,-43,-53,-114,-50,-105,-128,81,59,83,48,99,-62,-79,68,-50,-128,-16,22,74,66,-91,94,71,123,-116,96,-113,-102,-85,-107,-41,-33,-99,-115,-108,-98,22,90,28,40,116,22,75,-111,-26,-12,-88,-121,4,46,-104,-121,-45,21,47,-8,-24,-50,15,111,99,-85,-105,-94,-87,56,109,51,-73,111,-92,75,-79,18,-90,48,15,-62,90,40,22,-88,-125,-8,89,-27,-122,-26,116,-75,4,127,-68,17,79,52,-128,-54,-11,-87,-40,-2,-42,-32,107,87,73,26,-57,-115,113,-5,4,46,69,103,-13,111,-87,-80,36,-30,105,-119,100,114,73,-54,52,-37,-44,10,-120,-25,-54,122,-7,65,49,106,29,112,104,107,-53,45,67,-23,72,-127,83,53,-37,-36,-8,-122,-45,38,-20,-122,103,-55,91,60,12,-65,-15,81,-56,-60,37,-114,68,103,-65,95,42,-100,32,-83,-42,-104,47,-90,7,43,103,20,-28,5,-49,-18,122,-13,-109,-89,108,-2,-76,70,-21,0,7,-77,68,118,-51,59,6,97,-54,-4,113,-21,93,112,74,-88,29,66,5,-116,115,-53,-3,-126,-67,46,19,-79,-37,-15,-100,-109,27,-16,118,-54,85,-100,-93,-1,114,68,62,-39,-82,-114,75,85,-63,-116,-46,121,111,-80,32,72,-16,27,-78,86,-83,40,94,98,93,120,7,66,112,-26,49,-96,80,-96,-82,93,79,-93,57,83,-13,46,38,24,126,-43,32,-1,101,-110,71,-105,-100,96,-61,53,74,-118,-36,-113,-82,35,119,-122,90,40,-20,108,-122,5,71,60,-16,-112,-13,-7,-62,102,62,-124,-6,-35,-50,-29,-64,57,-110,83,96,-112,-42,-76,96,60,32,-122,-76,99,37,-106,-100,109,-33,33,-127,47,13,90,121,-104,-83,16,-50,23,61,31,96,67,-8,-19,105,-94,-66,-9,-73,2,-64,-64,-66,91,-107,-19,96,29,-107,11,-67,-108,-88,-25,95,85,71,85,-116,-33,-55,29,55,47,22,-95,63,-83,-116,-116,101,117,55,34,36,18,63,9,-96,79,-8,-18,94,-74,-91,-102,55,123,-116,-111,-128,-61,63,19,-89,-70,-67,42,-36,70,20,-88,61,-122,118,118,-32,-74,92,99,-96,125,85,45,-46,46,-95,-65,-22,13,9,-106,26,-21,-33,-105,-76,58,-98,-22,38,94,6,-34,79,71,78,-45,94,74,22,19,118,-19,126,-100,-63,12,-107,50,-54,58,37,74,-68,-49,65,108,112,61,96,48,23,38,65,-30,-21,34,50,-7,50,6,-42,53,-1,78,-18,44,57,112,-21,39,-127,-43,-63,-23,59,15,49,-72,-45,-40,-15,-62,104,-38,17,119,-125,113,26,-55,121,-40,-20,-115,87,-101,59,19,-1,109,-79,70,-102,-90,-113,-86,-96,-90,-97,35,0,-4,-86,-64,50,-11,33,-16,-31,-79,-97,-123,42,-81,-64,-108,86,-112,38,-56,42,-62,-89,24,86,-62,114,-83,6,75,126,83,-25,-87,-56,104,70,117,55,55,12,126,80,60,-71,-66,60,-55,111,44,-110,-122,-112,0,122,-85,37,52,-76,-102,16,124,84,-104,113,-21,-3,94,-8,16,35,76,-116,34,49,75,-9,51,83,32,-50,108,79,-21,95,-42,-90,-95,-45,72,105,-2,64,35,-69,57,-26,-74,-109,112,-81,-43,28,-116,38,90,-125,-103,104,64,63,-103,-22,10,98,-106,-28,-127,-121,-99,-86,48,71,64,-53,87,-80,81,47,74,55,-29,-56,-49,88,41,-91,-77,-21,43,1,-48,66,45,-127,19,65,16,107,-128,100,-68,10,126,10,63,104,-29,97,12,76,19,64,65,-34,-37,88,-5,3,-72,-42,-27,-66,71,-36,-67,64,-20,82,-109,-34,9,-43,1,50,-95,-62,81,-24,-19,3,58,24,117,-20,-51,-44,111,74,99,94,-43,-22,40,30,-118,-100,-11,32,75,4,-41,-123,37,116,-43,59,74,-89,-80,-53,-29,-65,41,-36,-93,-71,83,-128,3,-40,53,-47,-96,-113,21,-13,83,-12,-57,25,11,-20,-82,-93,-68,-100,114,-79,-23,76,63,-77,-64,-4,-104,34,106,114,-41,58,-114,-40,-109,-70,-119,-49,124,-66,-98,-56,84,66,-100,115,-83,105,-58,41,5,-48,-25,107,-93,-19,97,-53,40,-24,33,-111,41,-28,63,58,1,-63,-123,-17,43,-2,-24,-10,58,-68,6,28,-101,92,-62,9,-127,-79,-99,-23,37,-84,-30,-70,27,-48,-121,45,-56,-113,17,71,121,122,-37,11,-93,88,54,5,-11,-107,35,-3,33,2,55,112,51,-5,96,-98,-104,-54,43,-3,27,38,-64,-110,-120,-44,-59,9,65,-40,-94,75,38,108,26,-70,42,62,-38,-81,-18,58,23,-115,-59,-53,-109,92,-5,-18,-90,-50,57,15,76,-105,-99,74,-30,93,-20,-99,37,90,89,82,-122,120,45,-67,21,57,106,120,86,-39,83,82,42,-63,8,-89,13,-13,25,124,-51,-52,103,-127,30,-11,-34,113,122,-36,-54,-61,-39,-33,-113,-17,3,-79,124,1,52,7,-34,-106,-8,-5,103,115,109,100,-41,79,87,-37,-11,96,110,54,97,-98,48,-28,40,-70,-86,-60,-1,59,18,40,-26,1,71,40,88,-58,-43,89,-55,-124,108,-49,-20,66,-16,47,123,-77,5,101,110,-14,-113,104,-65,30,-126,97,-14,80,-78,-56,-46,13,8,10,104,52,-78,44,-123,50,-73,103,-110,-103,118,81,-126,-64,53,-39,126,-119,-97,69,1,40,-80,-85,83,122,56,-114,-73,25,-30,30,118,-69,-66,6,89,-92,5,101,-123,-21,-102,-78,-82,100,56,28,83,6,42,16,101,-26,24,-77,-61,-82,-16,-31,59,-15,-122,64,-92,126,-1,121,103,95,-70,-58,-127,120,-107,-53,-124,3,45,14,64,-46,-110,124,-81,71,12,56,-26,-7,8,110,-1,-36,106,67,41,113,-127,-89,-88,40,-127,125,-124,-112,68,125,125,-13,73,109,99,-40,-110,119,64,-40,79,49,111,-70,-64,40,-47,-23,-7,-86,74,-50,-92,-10,-2,57,-41,-64,-100,-57,84,-42,-56,-114,101,76,21,-2,81,121,25,-1,-68,-118,-66,-16,-55,-98,82,35,36,-127,-11,60,-63,49,29,2,28,47,106,58,-67,-29,-59,67,5,-115,-113,-100,-58,-83,-38,-21,-66,40,8,102,30,127,-94,-29,-32,-110,-98,-60,-34,45,-117,19,-12,-98,17,-58,36,-116,-32,-25,86,-89,-55,11,-115,10,50,-32,-97,-58,17,121,-6,-119,34,5,-78,36,12,71,-9,37,16,36,9,94,12,10,82,-123,-66,103,-106,58,-69,36,40,-43,70,-100,76,-63,-103,58,-52,-6,-100,67,-79,-7,93,6,-72,-65,-69,-55,31,-56,-58,-36,75,72,76,-31,-84,-127,57,-120,45,-88,22,-116,-53,-113,-101,86,18,35,-124,-32,-21,115,106,64,-104,-40,93,-52,-127,0,48,8,-42,-46,-41,14,43,105,-112,-22,64,38,-77,-52,-123,44,14,-86,-48,89,-10,-115,52,-82,-126,30,-98,4,103,-63,20,39,-10,-9,-98,-2,123,-64,107,-49,53,91,30,-19,-16,37,67,116,42,2,40,75,-56,-87,110,-124,-123,-38,-67,3,60,-50,-46,15,-66,-78,-88,95,-118,14,-15,-59,-34,23,-42,-106,77,113,105,-38,47,-98,53,32,-63,27,-44,-32,-73,-123,34,-91,61,87,43,-63,-117,-117,110,82,-29,-112,-117,8,-121,-108,76,97,66,88,6,-27,-53,22,-21,72,-83,-57,-108,17,99,-121,60,26";
            String[] split = byteStr.split(",");
            byte[] bytes = new byte[split.length];
            for (int i = 0; i < split.length; i++) {
                bytes[i] = (byte)Integer.parseInt(split[i]);
            }
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod("POST");
            httpRequest.setUrl(url);
            httpRequest.setRequestBody(HttpRequestBody.custom(bytes,"application/octet-stream;tt-data=a","utf-8"));

            Map<String,String> dataMap = Maps.newHashMap();
            try {
                String stub = md5(bytes);
//                dataMap.put("x-ss-stub",md5(bytes));
                Map<String, String> signatureMap = dySignature(downloader, url, dataMap);
                httpRequest.addHeader("User-Agent", "com.ss.android.ugc.aweme/140101 (Linux; U; Android 8.0.0; zh_CN; MI 5s; Build/OPR1.170623.032; Cronet/TTNetVersion:1c8b77ac 2020-12-16 QuicVersion:47946d2a 2020-10-14)");
                httpRequest.addHeader("Content-Type", "application/octet-stream;tt-data=a");
                httpRequest.getHeaders().putAll(signatureMap);
                httpRequest.addHeader("X-SS-STUB", stub);

            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            HttpPage download = downloader.download(httpRequest, httpConfig);
            System.out.println(download.getRawText());
        }
    }

    private static void device_register(HttpClientDownloader downloader,HttpConfig httpConfig,String openudid,String os_version,
                                        int os_api,String device_model,String device_brand,String device_manufacturer,
                                        String cpu_abi,String resolution,String build_serial,String mc,String mcc_mnc){

        String cdid = UUID.randomUUID().toString();
        String oaid = RandomStringUtils.random(16,"abcdef1234567890");


        String clientudid = UUID.randomUUID().toString();
        String req_id1 = UUID.randomUUID().toString();
        String req_id2 = UUID.randomUUID().toString();
        long time = System.currentTimeMillis()-1000*60*1L;
        long apk_first_install_time = System.currentTimeMillis()-1000*60*5L;
        long _gen_time = System.currentTimeMillis();
//        is_activated=1&
        String urlFormat = "https://log3-misc.amemv.com/service/2/device_register/?" +
                "ac=wifi&channel=huawei_1&aid=1128&app_name=aweme&version_code=140100&version_name=14.1.0&device_platform=android" +
                "&ssmix=a&device_type=%s&device_brand=%s&language=zh&os_api=%s&os_version=%s" +
                "&openudid=%s&manifest_version_code=140101&resolution=%s&dpi=480&update_version_code=14109900" +
                "&appTheme=dark&mcc_mnc=%s&cpu_support64=true&host_abi=%s&app_type=normal&cdid=%s&oaid=%s&tt_data=a";

        String url = String.format(urlFormat,device_model,device_brand,os_api,os_version,openudid,resolution,mcc_mnc,cpu_abi, cdid, oaid);

        Map<String,Object> requestBody = new HashMap<>();
        Map<String,Object> header = new HashMap<>();
        Map<String,Object> oaidMap = new HashMap<>();
        requestBody.put("magic_tag","ss_app_log");
        requestBody.put("_gen_time",_gen_time);
        requestBody.put("header",header);

        oaidMap.put("req_id",req_id1);
        oaidMap.put("hw_id_version_code","null");
        oaidMap.put("take_ms","43");
        oaidMap.put("is_track_limited","false");
        oaidMap.put("query_times","1");
        oaidMap.put("id",oaid);
        oaidMap.put("time",time);

        header.put("display_name","抖音");
        header.put("update_version_code","14109900");
        header.put("manifest_version_code","140101");
        header.put("app_version_minor","140101");
        header.put("aid","1128");
        header.put("channel","huawei_1");
        header.put("appkey","57bfa27c67e58e7d920028d3");
        header.put("package","com.ss.android.ugc.aweme");
        header.put("app_version","14.1.0");
        header.put("version_code","140100");
        header.put("sdk_version","2.14.0-rc.3-backoff");
        header.put("sdk_target_version",29);
        header.put("git_hash","490e7989");
        header.put("os","Android");
        header.put("os_version",os_version);
        header.put("os_api",os_api);
        header.put("device_model",device_model);
        header.put("device_brand",device_brand);
        header.put("device_manufacturer",device_manufacturer);
        header.put("cpu_abi",cpu_abi);
        header.put("build_serial",build_serial);
        header.put("release_build","804c624_20201222_24b2d6fa-4403-11eb-bfa5-02420a000038");
        header.put("density_dpi","280");
        header.put("display_density","mdpi");
        header.put("resolution",resolution);
        header.put("language","zh");
        header.put("mc",mc);
        header.put("timezone",8);
        header.put("access","wifi");
        header.put("not_request_sender",0);
        header.put("carrier","中国电信");
        header.put("mcc_mnc",mcc_mnc);
//        header.put("rom","MIUI-V11.0.2.0.OAGCNXM");
//        header.put("rom_version","MIUI-V11.0.2.0.OAGCNXM");
        header.put("cdid",cdid);
        header.put("sig_hash","aea615ab910015038f73c47e45d21466");
        header.put("openudid",openudid);
        header.put("clientudid",clientudid);
        header.put("serial_number",build_serial);
        header.put("sim_serial_number",new ArrayList<>());
        header.put("region","CN");
        header.put("tz_name","Asia/Shanghai");
        header.put("tz_offset",28800);
        header.put("sim_region","cn");
        header.put("req_id",req_id2);
        header.put("oaid",oaidMap);
        header.put("apk_first_install_time",apk_first_install_time);
        header.put("is_system_app",0);
        header.put("sdk_flavor","china");
        String param = JSON.toJSONString(requestBody);
        System.out.println(url);
        System.out.println(param);
        String str_data = StringUtils.join(toBytes(param),',');
        System.out.println(str_data);

        String registerResult = dyRegister(downloader,str_data);
        registerResult = registerResult.substring(1,registerResult.length()-1);
        System.out.println(registerResult);
        String result = deviceRegist(downloader, httpConfig, url, registerResult);
        Map map = JSON.parseObject(result, Map.class);
        String device_id = map.get("device_id").toString();
        String iid = map.get("install_id").toString();
        if ("0".equals(device_id) || "0".equals(iid)){
            System.out.println("注册失败的AndroidID：" + openudid);
        } else {
            String rawText = donwloadTest(downloader, httpConfig, cdid, device_id, device_model, iid, openudid, String.valueOf(os_api), os_version, resolution);
            System.out.println(rawText);

            Map<String,String> deviceMap = new HashMap<>();
            deviceMap.put("openudid",openudid);
            deviceMap.put("os_version",os_version);
            deviceMap.put("os_api",String.valueOf(os_api));
            deviceMap.put("device_model",device_model);
            deviceMap.put("device_brand",device_brand);
            deviceMap.put("device_manufacturer",device_manufacturer);
            deviceMap.put("cpu_abi",cpu_abi);
            deviceMap.put("resolution",resolution);
            deviceMap.put("build_serial",build_serial);
            deviceMap.put("mc",mc);
            deviceMap.put("mcc_mnc",mcc_mnc);
            deviceMap.put("device_id",device_id);
            deviceMap.put("iid",iid);
            deviceMap.put("cdid",cdid);
            deviceMap.put("oaid",oaid);
            deviceMap.put("clientudid",clientudid);
            deviceMap.put("category","device_comment");
            System.out.println(JSON.toJSONString(deviceMap));

            if (StringUtils.isNotBlank(rawText)){
                JSONObject jsonObject = JSONObject.parseObject(rawText);
                JSONArray awemeList = jsonObject.getJSONArray("aweme_list");
                if (null != awemeList && awemeList.size() > 0){
                    //保存设备信息
                    saveDevice(deviceMap);
                    System.out.println("保存成功！");
                }else {
                    System.out.println("注册失败的AndroidID：" + deviceMap.get("openudid"));
                }

            }else {
                System.out.println("注册失败的AndroidID：" + deviceMap.get("openudid"));
            }
        }
    }

    private static void saveDevice(Map<String, String> deviceMap) {
        String device = JSON.toJSONString(deviceMap);
        File file = new File(filePathName);
        try {
            if (!file.exists()){
                file.createNewFile();
            }
            BufferedWriter output = new BufferedWriter(new FileWriter(file,true));
            output.write(device);
            output.write("\r\n");
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static String donwloadTest(HttpClientDownloader downloader,HttpConfig httpConfig,String cdid,String device_id,
                                     String device_type,String iid,String openudid,String os_api,String os_version, String resolution){
        String channel = "huawei_"+ RandomUtils.nextInt(20,90);

        String common_param_v2 = "aid=1128&app_name=aweme&app_type=normal&cdid=%s&channel="+channel+"&device_brand=samsung" +
                "&device_id=%s&device_platform=android&device_type=%s&dpi=480&iid=%s&language=zh&manifest_version_code=140101" +
                "&openudid=%s&os_api=%s&os_version=%s&resolution=%s&ssmix=a&update_version_code=14109900" +
                "&version_code=140100&version_name=14.1.0";

        HttpRequest httpRequest = new HttpRequest();
        long rticket = System.currentTimeMillis();
        long ts = rticket / 1000;

        String accountUrlParamFormat = "source=0&user_avatar_shrink=96_96&video_cover_shrink=248_330" +
                "&publish_video_strategy_type=2&max_cursor=0" +
                "&sec_user_id=MS4wLjABAAAAWFV4r4bm27prbxuX5xE7JljwCD5vh4SN3V_VxZd-_tQ&count=20" +
                "&is_order_flow=0&page_from=2&longitude=121.5888983509045&latitude=31.25251444047919" +
                "&location_permission=true&_rticket="+rticket+"&cpu_support64=true&host_abi=armeabi-v7a" +
                "&appTheme=dark&ac=wifi&ts="+ts+"&";

//        String urlParam = String.format(accountUrlParamFormat, 0, "MS4wLjABAAAAWFV4r4bm27prbxuX5xE7JljwCD5vh4SN3V_VxZd-_tQ", rticket, ts);
        String url = "https://api3-core-c-lf.amemv.com/aweme/v1/aweme/post/?" + accountUrlParamFormat;
        httpRequest.setUrl(url);

        String common_params = String.format(common_param_v2,cdid,device_id,device_type,iid,openudid,os_api,os_version,resolution);
        httpRequest.addHeader("User-Agent", "com.ss.android.ugc.aweme/140101 (Linux; U; Android 7.1.2; zh_CN; SM-G9550;" +
                " Build/OPR1.170623.032; Cronet/TTNetVersion:"+RandomStringUtils.random(8,"0123456789abcdf")+" 2020-12-16 QuicVersion:"+RandomStringUtils.random(8,"0123456789abcdf")+" 2020-10-14)");
        httpRequest.addHeader("x-common-params-v2", common_params);
        System.out.println(common_params);
        Map<String,String> dataMap = new HashMap<>();
        Map<String, String> signatureMap = dySignature(downloader, url,dataMap);

        httpRequest.getHeaders().putAll(signatureMap);
        HttpPage download = null;
        String rawText = null;
        try {
            download = downloader.download(httpRequest,httpConfig);
            if (!download.isDownloadSuccess()){
                throw new Exception("download error!");
            }
            rawText = download.getRawText();
        } catch (Exception e) {
            try {
                while (download == null || !download.isDownloadSuccess()){
                    Thread.sleep(3000);
                    download = downloader.download(httpRequest, httpConfig);
                    rawText = download.getRawText();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return rawText;
    }

    private static byte[] toBytes(String param){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(param.getBytes());
            gzip.close();
        } catch (IOException e) {
        }
        byte[] bytes = out.toByteArray();
        return bytes;
    }

    private static void device_register(String build_serial, String openudid, String address){

        HttpConfig httpConfig = HttpConfig.me("test");
        httpConfig.setProxy(proxy);
        httpConfig.setUseProxy(true);

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        String os_version = "7.1.2";
        int os_api = 25;
        String device_model = "SM-G9550";
        String device_brand = "samsung";
        String device_manufacturer = "samsung";
        String cpu_abi = "armeabi-v7a";
        String resolution = "720x1280";
        String mc = address.toUpperCase();
        String mcc_mnc = "46007";

        device_register(downloader,httpConfig,openudid,os_version,os_api,device_model,device_brand,
                device_manufacturer,cpu_abi,resolution,build_serial,mc,mcc_mnc);


    }

    private static void device_register(){

        HttpConfig httpConfig = HttpConfig.me("test");
        httpConfig.setProxy(proxy);
        httpConfig.setUseProxy(true);
//
        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        String openudid = "92b16d4c2ededfd8";
        String os_version = "8.0.0";
        int os_api = 26;
        String device_model = "MI+5s";
        String device_brand = "Xiaomi";
        String device_manufacturer = "Xiaomi";
        String cpu_abi = "arm64-v8a";
        String resolution = "1080x1920";
        String build_serial = "1793bb1c";
        String mc = "34:80:b3:fb:5a:d1".toUpperCase();
        String mcc_mnc = "9460";

        device_register(downloader,httpConfig,openudid,os_version,os_api,device_model,device_brand,
                device_manufacturer,cpu_abi,resolution,build_serial,mc,mcc_mnc);

//        String openudid = "c839e5a342685cde";
//        String os_version = "6.0";
//        int os_api = 23;
//        String device_model = "DIG-AL00";
//        String device_brand = "HUAWEI";
//        String device_manufacturer = "HUAWEI";
//        String cpu_abi = "arm64-v8a";
//        String resolution = "720x1280";
//        String build_serial = "DWH9X17321W18759";
//        String mc = "14:5F:94:3D:B1:23".toUpperCase();
//
//        device_register(downloader,httpConfig,openudid,os_version,os_api,device_model,device_brand,
//                device_manufacturer,cpu_abi,resolution,build_serial,mc);

    }

    private static String randomMACAddress(){
        Random rand = new Random();
        byte[] macAddr = new byte[6];
        rand.nextBytes(macAddr);

        macAddr[0] = (byte)(macAddr[0] & (byte)254);  //zeroing last 2 bytes to make it unicast and locally adminstrated

        StringBuilder sb = new StringBuilder(18);
        for(byte b : macAddr){

            if(sb.length() > 0)
                sb.append(":");

            sb.append(String.format("%02x", b));
        }


        return sb.toString().toUpperCase();
    }

    private static void copyRedisDataSet(){
        RedisReader redisReader = new RedisReader("192.168.1.214", 6379, 3);
        Jedis jedis=new Jedis("192.168.1.214", 6379);
        jedis.select(3);

        List<String> smembers = jedis.lrange("api-dy-article-ids-queue-bak",0,-1);
        for (String smember : smembers) {
            jedis.lpush("api-dy-article-ids-queue",smember);
        }


    }
}
